using System;
using System.Windows;
using DotBeer.Business.Components;
using DotBeer.UI.WPFClient.Commands;
using DotBeer.Business.Entities;
using System.Windows.Input;
using System.Diagnostics;

namespace DotBeer.UI.WPFClient.ViewModels
{
    public class EditViewModel : HeroViewModelBase
    {
        #region private members

        DelegateCommand _addNewItemBuildCommand;
        DelegateCommand _addNewSkillBuildCommand;
        DelegateCommand _deleteBuildCommand;
        DelegateCommand _addNewBuildCommand;
        DelegateCommand _clearItemBuildCommand;
        DelegateCommand _clearSkillBuildCommand;
        DelegateCommand _saveBuildCommand;
        DelegateCommand<Item> addItemCommand;

        #endregion

        #region constructors

        public EditViewModel(string heroName)
        {
            this.LoadHero(() =>
            {
                return new Hero()
                            {
                                Name = heroName,
                                Author = ""
                            };
            });
        }

        public EditViewModel(string heroName, string authorName)
        {
            this.LoadHero(heroName, authorName);
        }

        #endregion

        #region commands

        public DelegateCommand<Item> AddItemCommand
        {
            get
            {
                if (this.addItemCommand == null)
                    this.addItemCommand = new DelegateCommand<Item>(this.AddItem);

                return this.addItemCommand;
            }
        }

        public DelegateCommand AddNewItemBuildCommand
        {
            get
            {
                if (_addNewItemBuildCommand == null)
                    _addNewItemBuildCommand = new DelegateCommand(this.AddNewItemBuild);

                return _addNewItemBuildCommand;
            }
        }

        public DelegateCommand AddNewSkillBuildCommand
        {
            get
            {
                if (_addNewSkillBuildCommand == null)
                    _addNewSkillBuildCommand = new DelegateCommand(this.AddNewSkillBuild);

                return _addNewSkillBuildCommand;
            }
        }

        public DelegateCommand DeleteBuildCommand
        {
            get
            {
                if (_deleteBuildCommand == null)
                    _deleteBuildCommand = new DelegateCommand(this.DeleteBuild);

                return _deleteBuildCommand;
            }
        }

        public DelegateCommand AddNewBuildCommand
        {
            get
            {
                if (_addNewBuildCommand == null)
                    _addNewBuildCommand = new DelegateCommand(this.AddNewBuild);

                return _addNewBuildCommand;
            }
        }

        public DelegateCommand ClearSkillBuildCommand
        {
            get
            {
                if (_clearSkillBuildCommand == null)
                    _clearSkillBuildCommand = new DelegateCommand(this.ClearSkillBuild, this.CanClearSkillBuild);

                return _clearSkillBuildCommand;
            }
        }

        public DelegateCommand ClearItemBuildCommand
        {
            get
            {
                if (_clearItemBuildCommand == null)
                    _clearItemBuildCommand = new DelegateCommand(this.ClearItemBuild, this.CanClearItemBuild);

                return _clearItemBuildCommand;
            }
        }

        public DelegateCommand SaveBuildCommand
        {
            get
            {
                if (_saveBuildCommand == null)
                    _saveBuildCommand = new DelegateCommand(this.SaveBuild, this.CanSaveBuild);

                return _saveBuildCommand;
            }
        }

        #endregion

        #region private methods

        private void AddItem(Item item)
        {
            this.hero.CurrentItems.Add(new ItemHero()
                                                {
                                                    Name = item.Name,
                                                    ItemsCollection = item.ItemsCollection
                                                });
        }

        private void AddNewItemBuild()
        {
            hero.AddNewItemBuild();
        }

        private void AddNewSkillBuild()
        {
            hero.AddNewSkillBuild();
        }

        private void DeleteBuild()
        {
            if (CustomMessageBox.Show("Are you sure delete this build? You won't be able to undo this operation",
                                      "Confirm delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {

                try
                {
                    HeroComponent.Delete(hero.Name, hero.Author);
                    CustomMessageBox.Show("Build deleted.", "Information");
                }
                catch (Exception ex)
                {
                    CustomMessageBox.Show(String.Format("Error occured while deleting hero build: \n{0}", ex.Message), "Error");
                }

                hero.Clear();
            }
        }

        private void AddNewBuild()
        {
            hero.Clear();
        }

        private void ClearSkillBuild()
        {
            hero.CurrentSkills.Clear();
        }

        private bool CanClearSkillBuild()
        {
            return hero.CurrentSkills != null;
        }

        private void ClearItemBuild()
        {
            hero.CurrentItems.Clear();
        }

        private bool CanClearItemBuild()
        {
            return hero.CurrentItems != null;
        }

        private void SaveBuild()
        {
            try
            {
                HeroComponent.Save(hero);
                CustomMessageBox.Show("Saved successful.", "Information");
            }
            catch (Exception ex)
            {
                CustomMessageBox.Show(String.Format("Error occured while saving hero build: \n{0}", ex.Message), "Error");
            }
        }

        private bool CanSaveBuild()
        {
            if (hero == null)
                return false;
            else
            {
                if (hero.Author == null)
                    return false;
                else
                    if (hero.Author.Trim().Length == 0)
                        return false;
            }

            return true;
        }

        #endregion
    }
}